//go:build integration
// +build integration

package main

import (
	"context"
	"flag"
	"fmt"
	"io/ioutil"
	"strconv"
	"strings"
	"time"

	"git.bybit.com/svc/stub/pkg/pb/api/kycadmin"
	"google.golang.org/grpc"
)

func main() {
	isFile := flag.Bool("file", true, "file parameter")
	filePath := flag.String("path", "/Users/gz99008ml/yijin-code/kyc_admin_service/cmd/cli/tool/lv2uid.txt", "path parameter")
	grpcAddr := flag.String("grpc", "kyc-admin-service-cht-test-1.test.efficiency.ww5sawfyut0k.bitsvc.io:9090", "grpc parameter")
	memberID := flag.Int64("uid", 0, "uid parameter")
	flag.Parse()

	memberIDArr := []int64{}
	if *isFile {
		data, err := ioutil.ReadFile(*filePath)
		if err != nil {
			panic(err)
		}
		uidStrArr := strings.Split(string(data), "\n")
		for _, uidStr := range uidStrArr {
			uidInt, _ := strconv.ParseInt(uidStr, 10, 64)
			if uidInt > 0 {
				memberIDArr = append(memberIDArr, uidInt)
			} else {
				fmt.Println("strconv.ParseInt uid failed, uid: ", uidStr)
			}
		}
	} else if *memberID > 0 {
		memberIDArr = append(memberIDArr, *memberID)
	}

	if len(memberIDArr) == 0 {
		panic("memberIDArr len is 0")
	}
	conn, err := grpc.Dial(*grpcAddr, grpc.WithInsecure(), grpc.WithBlock())
	if err != nil {
		panic(err)
	}
	defer conn.Close()
	fmt.Println("begin call rpc, ", "grpc:", *grpcAddr)
	totalCnt := len(memberIDArr)
	successCnt := []int64{}
	failedCnt := []int64{}
	for _, uid := range memberIDArr {
		c := kycadmin.NewKycAdminInnerServiceClient(conn)
		ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
		defer cancel()

		req := &kycadmin.AppendLevel2DataRequest{
			MemberId: uid,
		}
		resp, err := c.AppendLevel2Data(ctx, req)
		if err != nil {
			failedCnt = append(failedCnt, uid)
			fmt.Println("AppendLevel2Data error", "memberID:", uid, err)
		} else {
			if resp.GetResult() {
				successCnt = append(successCnt, uid)
			} else {
				failedCnt = append(failedCnt, uid)
				fmt.Println("AppendLevel2Data failed", "memberID:", uid)
			}
		}
	}
	fmt.Println("end call rpc")
	fmt.Println("\n")
	fmt.Println(fmt.Sprintf("totalCnt: %d, successCnt: %d, failedCnt: %d", totalCnt, len(successCnt), len(failedCnt)))
}
